package data.book;

import java.util.ArrayList;

public class Keyword {
	private String keyword;
	private ArrayList<String> spilitWords;
	
	public Keyword(String keyword){
		this.keyword=keyword;
		spilitWords=new ArrayList<String>();
		spilitKeyWord();
	}
	
	public int getAccuracy(String name){
		
		int accuracy=0;
		if(name.equalsIgnoreCase(keyword)){
			accuracy=spilitWords.size()+2;
		}
		else if(contains(name, keyword)){
			accuracy=spilitWords.size()+1;
		}
		else{
			for (int i = 0; i < spilitWords.size(); i++) {
				if(contains(name, spilitWords.get(i))){
					accuracy++;
				}
			}
		}
		
		
		
		return accuracy;
		
	}
	
	private boolean contains(String container,String key){
		return container.toLowerCase().contains(key.toLowerCase());
	}
	
	
	public int getSpilitWordsSize(){
		return spilitWords.size();
	}
	
	private void spilitKeyWord(){
		String[] spilit_space=keyword.split(" ");
		for (int i = 0; i < spilit_space.length; i++) {
			spilitWord(spilit_space[i]);
		}
	}
	
	private void spilitWord(String word){
		char[] spilit=word.toCharArray();
		
		String unit;
		for (int i = 0; i < spilit.length; i++) {
			unit="";
			char judge_char=spilit[i];
			boolean isABC=isABC(judge_char);
			if(!isABC){
				unit=judge_char+"";
			}
			else{
				boolean judge_while=true;
				do{
					unit=unit.concat(spilit[i]+"");
					i++;
					if(i < spilit.length)
						judge_while=isABC(spilit[i]);
					else
						judge_while=false;
				}while(judge_while);
				i--;
			}
			
			spilitWords.add(unit);
		}
	}
	
	private boolean isABC(char judge_char){
		return (judge_char>='a'&&judge_char<='z')
				||(judge_char>='A'&&judge_char<='Z');
	}
}
